﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Resources;
using System.Runtime.Caching;
using System.Web;
using System.Web.Compilation;

namespace blackhouse.Translator.Web {
    public class GlobalFallbackResourceProvider : IResourceProvider {

        #region Fields

        private FalbackResourceClass classManager;

        #endregion

        #region Constructors

        public GlobalFallbackResourceProvider(string classKey) {
            string cKey = "ClassManager-" + classKey;
            this.classManager = (FalbackResourceClass)MemoryCache.Default[cKey];
            if (this.classManager == null) {
                this.classManager = new FalbackResourceClass(classKey);
                CacheItem ci = new CacheItem(cKey,this.classManager);
                CacheItemPolicy cip = new CacheItemPolicy();
                cip.SlidingExpiration = TimeSpan.FromHours(1);
                HostFileChangeMonitor cMonitor = new HostFileChangeMonitor(this.classManager.GetFilesPath());
                cip.ChangeMonitors.Add(cMonitor);
                MemoryCache.Default.Add(ci, cip);
            }
        }

        #endregion

        #region Methods

        public object GetObject(string resourceKey, CultureInfo culture) {
            if (culture == null)
                culture = CultureInfo.CurrentCulture;
            return this.classManager.GetValue(resourceKey, culture, CultureInfo.GetCultureInfo("en-GB"));
        }

        public System.Resources.IResourceReader ResourceReader {
            get { throw new NotImplementedException(); }
        }

        #endregion

    }
}